package de.bbl.android.fragment.DataCenter.Table;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import de.bbl.android.BBLAndroidFragmentActivity;
import de.bbl.android.R;
import de.bbl.android.adapter.DataCenter.DataCenterTableArrayAdapter;
import de.bbl.android.classes.TableTeam;
import de.bbl.android.tools.Tools;

public class DataCenterTableFragment extends ListFragment{
	Activity activity;
	TableTeam[] currentLoaded = null;
	private boolean finished = true;
	private boolean isDetached = false;

	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isDetached = false;
        activity = getActivity();
        if (savedInstanceState == null || currentLoaded == null) {
        	RefreshData();
        }
    }
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// super.onCreateOptionsMenu(menu, inflater);
    	inflater.inflate(R.menu.activity_bblmain_refresh, menu);
    	
    	// remove Refresh from OptionsMenu cause is implemented in titlebar
    	if (android.os.Build.VERSION.SDK_INT < 11)
    		menu.getItem(0).setVisible(false);
	}
	
	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
	}

    @Override    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.Refresh:
            	RefreshData();
            	break;
        }
        return true;
    }
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		
		ListView temp = getListView();
		if (getListAdapter() == null) {
		//if (getListAdapter().isEmpty()) {
			View headerView = getActivity().getLayoutInflater().inflate(R.layout.row_layout_datacenter_table_header, null);
			temp.addHeaderView(headerView);
		} else {
			setListAdapter(null);
			View headerView = getActivity().getLayoutInflater().inflate(R.layout.row_layout_datacenter_table_header, null);
			temp.addHeaderView(headerView);
		}
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}
    
    public void RefreshData() {
        setHasOptionsMenu(true);
        if (activity != null) {
    		startProgress(activity.getCurrentFocus());
        }
    }
    
	private void loadData() {
		String[] dataArrayUrl = getResources().getStringArray(R.array.Data_Files_Url);
		String[] dataArray = getResources().getStringArray(R.array.Data_Files);
		String dataUrl = "";
		String data = "";
		dataUrl = dataArrayUrl[BBLAndroidFragmentActivity.DATA_STANDINGS];
		data = dataArray[BBLAndroidFragmentActivity.DATA_STANDINGS];
		Tools.getGamedayData(getResources().getString(R.string.main_url), dataUrl, data, BBLAndroidFragmentActivity.bbl_temp_sd_path);
	}
    
    private void startProgress(final View view) {
    	final Handler refresh = new Handler(Looper.getMainLooper());
    	Runnable runnable = new Runnable() {			
			public void run() {
				setFinished(false);
				String[] dataArray = getResources().getStringArray(R.array.Data_Files);
				loadData();
				currentLoaded = de.bbl.android.tools.XmlParseTableTeamTools.XmlParseTableTeam(BBLAndroidFragmentActivity.bbl_temp_sd_path + dataArray[BBLAndroidFragmentActivity.DATA_STANDINGS]);
				refresh.post(new Runnable() {
					public void run() {
						if (! isDetached) {
							representTableTeamData(view);
						} 
						setFinished(true);
					}
				});				
			}
		};		
		new Thread(runnable).start();
    }
    
    private void representTableTeamData(View view) {
		Log.d("bbl-debug", "Spielplan Fagrment detached: " + isDetached);

    	if (currentLoaded != null && ! isDetached) {
    		/*
    		ListView temp = getListView();
    		if (temp.getHeaderViewsCount() == 0) {
    			View headerView = activity.getLayoutInflater().inflate(R.layout.row_layout_datacenter_table_header, null);
    			temp.addHeaderView(headerView);

    		}
    		*/
    		setListAdapter(new DataCenterTableArrayAdapter(activity, currentLoaded));
    	}
    		
    }

	public boolean isFinished() {
		return finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	@Override
	public void onDestroyView() {
		isDetached = true;
		super.onDestroyView();
	}
	
}
